/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.core.action.retrievedefault;

import com.inspur.edp.bff.api.dataprocessor.IDefaultValueConvertor;
import com.inspur.edp.bff.api.manager.assembler.IAssemblerManager;
import com.inspur.edp.bff.api.manager.context.DataMapperContext;
import com.inspur.edp.bff.api.manager.context.RetrieveDefaultContext;
import com.inspur.edp.bff.entity.defaultvalue.VoDefaultValue;
import com.inspur.edp.bff.spi.AbstractFSAction;
import com.inspur.edp.cef.entity.entity.IEntityData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class RetrieveDefaultWithChildAction extends AbstractFSAction<IEntityData> {


    private VoDefaultValue defaultValue;
    private String dataId;
    private LinkedHashMap<String, Integer> childCount;

    public RetrieveDefaultWithChildAction(VoDefaultValue defaultValue, LinkedHashMap<String, Integer> childCount) {
        this.defaultValue = defaultValue;
        this.childCount = childCount;
    }

    @Override
    protected void execute() {
        IAssemblerManager assemblerManager = getContext().getAssemblerManager();
        if (assemblerManager == null) {
            doDefaultRetrieveDefault();
            return;
        }
        RetrieveDefaultContext createContext = new RetrieveDefaultContext();
        createContext.setVoDefaultValue(defaultValue);
        createContext.setDataId(dataId);
        //① BeforeRetrieve
        assemblerManager.getRetrieveDefaultAssembler().doBeforeRetrieveDefault(getContext(), createContext);

        //② DoRetrieve
        assemblerManager.getRetrieveDefaultAssembler().doRetrieveDefault(getContext(), createContext);
        //③ DataMapping
        DataMapperContext mapperContext = new DataMapperContext();
        mapperContext.setSourceData(createContext.getRetrieveDefaultData());
        mapperContext.setIsNew(true);
        mapperContext.setVoDefaultValue(defaultValue);
        ArrayList<IEntityData> vmDatas = assemblerManager.getDataMapperAssembler().
                doDataMapping2Vo(getContext(), mapperContext);
        createContext.setRetrieveDefaultResult(vmDatas.get(0));
        //④ AfterRetrieve
        assemblerManager.getRetrieveDefaultAssembler().doAfterRetrieveDefault(getContext(), createContext);
        retrieveChild(vmDatas.get(0));
        setResult(vmDatas.get(0));
    }

    private void retrieveChild(IEntityData vmData) {
        for (Map.Entry entry : childCount.entrySet()) {
            ArrayList<String> nodeCodes = new ArrayList<>();
            ArrayList<String> lierachyIds = new ArrayList<>();
            nodeCodes.add(entry.getKey().toString());
            lierachyIds.add(vmData.getID());
            RetrieveDefaultChildAction childAction = new RetrieveDefaultChildAction(nodeCodes, lierachyIds);
            int count = Integer.parseInt(entry.getValue().toString());
            for (int i = 0; i < count; i++) {
                IEntityData childData = childAction.deal(getContext());
                vmData.getChilds().get(nodeCodes.get(0)).add(childData);
            }
        }

    }

    private void doDefaultRetrieveDefault() {
        IEntityData beData;

        IDefaultValueConvertor convertor = getContext().getDefaultValueConvertor();
        if (defaultValue != null && convertor != null) {

            HashMap<String, Object> beDefault = convertor.convertFromVo(defaultValue);
            beData = getLcp().retrieveDefault(beDefault);
        } else if (!(dataId == null || dataId.isEmpty())) {
            beData = getLcp().retrieveDefault(dataId);
        } else {
            beData = getLcp().retrieveDefault();
        }

        IEntityData vmData = getContext().createData();
        getContext().getDataConvertor().convertFromBEData(beData, vmData);
        setResult(vmData);
    }


}
